package com.employment.controller;

import com.employment.common.R;
import com.employment.entity.ChatTemplate;
import com.employment.service.ChatTemplateService;
import com.employment.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 常用回复模板控制器
 * 
 * @author employment
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/api/chat/template")
@Tag(name = "常用回复模板管理")
@Validated
public class ChatTemplateController {
    
    @Autowired
    private ChatTemplateService templateService;
    
    @Operation(summary = "创建常用回复模板")
    @PostMapping
    public R<ChatTemplate> createTemplate(
            @NotBlank(message = "模板标题不能为空") @RequestParam String title,
            @NotBlank(message = "模板内容不能为空") @RequestParam String content,
            @RequestParam(required = false) String category) {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            
            ChatTemplate template = templateService.createTemplate(currentUserId, title, content, category);
            
            return R.success(template);
            
        } catch (Exception e) {
            log.error("创建常用回复模板失败", e);
            return R.error("创建常用回复模板失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "更新常用回复模板")
    @PutMapping("/{templateId}")
    public R<ChatTemplate> updateTemplate(
            @NotNull(message = "模板ID不能为空") @PathVariable Long templateId,
            @NotBlank(message = "模板标题不能为空") @RequestParam String title,
            @NotBlank(message = "模板内容不能为空") @RequestParam String content,
            @RequestParam(required = false) String category) {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            
            ChatTemplate template = templateService.updateTemplate(templateId, currentUserId, title, content, category);
            
            return R.success(template);
            
        } catch (Exception e) {
            log.error("更新常用回复模板失败", e);
            return R.error("更新常用回复模板失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "删除常用回复模板")
    @DeleteMapping("/{templateId}")
    public R<Void> deleteTemplate(@NotNull(message = "模板ID不能为空") @PathVariable Long templateId) {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            
            templateService.deleteTemplate(templateId, currentUserId);
            
            return R.success();
            
        } catch (Exception e) {
            log.error("删除常用回复模板失败", e);
            return R.error("删除常用回复模板失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "获取常用回复模板列表")
    @GetMapping
    public R<List<ChatTemplate>> getTemplates(@RequestParam(required = false) String category) {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            
            List<ChatTemplate> templates = templateService.getUserTemplates(currentUserId, category);
            
            return R.success(templates);
            
        } catch (Exception e) {
            log.error("获取常用回复模板列表失败", e);
            return R.error("获取常用回复模板列表失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "搜索常用回复模板")
    @GetMapping("/search")
    public R<List<ChatTemplate>> searchTemplates(@RequestParam String keyword) {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            
            List<ChatTemplate> templates = templateService.searchTemplates(currentUserId, keyword);
            
            return R.success(templates);
            
        } catch (Exception e) {
            log.error("搜索常用回复模板失败", e);
            return R.error("搜索常用回复模板失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "使用模板（增加使用次数）")
    @PostMapping("/{templateId}/use")
    public R<Void> useTemplate(@NotNull(message = "模板ID不能为空") @PathVariable Long templateId) {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            
            templateService.incrementUsageCount(templateId, currentUserId);
            
            return R.success();
            
        } catch (Exception e) {
            log.error("使用模板失败", e);
            return R.error("使用模板失败：" + e.getMessage());
        }
    }
    
    @Operation(summary = "获取常用模板")
    @GetMapping("/frequent")
    public R<List<ChatTemplate>> getFrequentlyUsedTemplates(
            @RequestParam(defaultValue = "10") Integer limit) {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            
            List<ChatTemplate> templates = templateService.getFrequentlyUsedTemplates(currentUserId, limit);
            
            return R.success(templates);
            
        } catch (Exception e) {
            log.error("获取常用模板失败", e);
            return R.error("获取常用模板失败：" + e.getMessage());
        }
    }
}